import { createPinia, defineStore } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";

import { walletSelectApi } from "@/api/payServerse/wallet";
import { MerListApi } from "@/api/ExAssistant/merchant";
import { payMerListApi } from "@/api/payServerse/merchant";

export const GlobalStore = defineStore("GlobalStore", {
  state: () => {
    return {
      //token
      token: "",
      // 折叠菜单
      isCollapse: false,
      coins: [], //币种
      chains: [], //主链
      tokens: [], //Tokens
      trades: [], //交易
      merchantList: [], //商户
      payMerchantList: [], //商户
    };
  },
  getters: {},
  actions: {
    setToken(token) {
      this.token = token;
    },
    setIsCollapse() {
      this.isCollapse = !this.isCollapse;
    },

    async walletSelectConfig() {
      try {
        let res = await walletSelectApi();
        const { coins, chains, tokens, trades } = res.data;
        this.coins = coins;
        this.chains = chains;
        this.tokens = tokens;
        this.trades = trades;
      } catch (error) {
        return error;
      }
    },
    async setMerchant() {
      try {
        let res = await MerListApi({ page: 1, pagesize: 9999 });
        this.merchantList = res.data.results || [];
      } catch (error) {
        return error;
      }
    },
    async setPayMerchant() {
      try {
        let res = await payMerListApi({ page: 1, pagesize: 9999 });
        this.payMerchantList = res.data.results || [];
      } catch (error) {
        return error;
      }
    },
    clearData() {
      this.token = "";
      this.isCollapse = false;
      this.coins = [];
      this.chains = [];
      this.tokens = [];
      this.trades = [];
      this.merchantList = [];
      this.payMerchantList = [];
    },
  },
  persist: true,
});

const store = createPinia();
store.use(piniaPluginPersistedstate);
export default store;
